package com.appbee.gdata.picasa.client.ui;

import javax.inject.Inject;

import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.process.LoadingIndcator;
import com.appbee.gdata.picasa.client.activity.PicasaAlbumActivity;
import com.appbee.gdata.picasa.client.text.PicasaStrings;
import com.appbee.gdata.picasa.client.ui.design.AlbumListResources;
import com.appbee.gdata.picasa.shared.SimpleAlbumProxy;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class AlbumView extends Composite implements PicasaAlbumActivity.View
{

	private static AlbumViewUiBinder uiBinder = GWT
			.create(AlbumViewUiBinder.class);

	interface AlbumViewUiBinder extends UiBinder<Widget, AlbumView>
	{
	}

	@UiField(provided = true)
	PageStyle style;

	@UiField(provided = true)
	CellList<SimpleAlbumProxy> cellList;

	@UiField(provided = true)
	SimplePager pager;

	private PicasaAlbumActivity activity;

	private final PicasaMediaHtmlTemplate template;
	private final SingleSelectionModel<SimpleAlbumProxy> selectionModel;

	private final AlbumListResources albumListRes;

	@UiField
	PicasaStrings picasaStrings;

	@Inject
	public AlbumView(	PageStyle style,
						AlbumListResources albumListRes,
						PicasaMediaHtmlTemplate template,
						LoadingIndcator loadingIndcator,
						PicasaStrings picasaStrings)
	{
		this.style = style;
		this.albumListRes = albumListRes;
		this.template = template;
		this.picasaStrings = picasaStrings;
		cellList = new CellList<SimpleAlbumProxy>(new MediaThumbnailCell(),
				albumListRes);
		cellList.setLoadingIndicator(loadingIndcator);
		selectionModel = new SingleSelectionModel<SimpleAlbumProxy>();
		cellList.setSelectionModel(selectionModel);
		pager = new SimplePager();
		pager.setDisplay(cellList);
		initWidget(uiBinder.createAndBindUi(this));

		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler()
				{

					@Override
					public void onSelectionChange(SelectionChangeEvent event)
					{
						SimpleAlbumProxy selectedObject = selectionModel
								.getSelectedObject();
						if (selectedObject != null)
							activity.onAlbumSelected(selectedObject);
					}
				});
	}

	@Override
	public void setActivity(PicasaAlbumActivity activity)
	{
		this.activity = activity;
	}

	@Override
	public HasData<SimpleAlbumProxy> getHasData()
	{
		return cellList;
	}

	@Override
	public void clearSelection()
	{
		SimpleAlbumProxy selectedObject = selectionModel.getSelectedObject();
		if (selectedObject != null)
			selectionModel.setSelected(selectedObject, false);
	}

	public class MediaThumbnailCell extends AbstractCell<SimpleAlbumProxy>
	{
		public MediaThumbnailCell()
		{
			super("click");

		}

		@Override
		public void render(	Context context,
							SimpleAlbumProxy value,
							SafeHtmlBuilder sb)
		{
			if (value != null)
			{
				String title = value.getTitle() == null ? "" : value
												.getTitle();
				sb.append(template.img(	title,
										value.getThumbnail().getUrl()));
				sb.append(template.div(albumListRes.cellListStyle()
						.albumTitle(), title));
			}
		}
	}

}
